package com.wangxl.girl.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wangxl.girl.domain.Girl;
import com.wangxl.girl.repository.GirlRepository;
import com.wangxl.girl.service.GirlService;

@RestController
public class GirlController
{
	@Autowired
	private  GirlRepository girlRepository;
	@Autowired
	private  GirlService girlService;
	/**
	 * 查询女生列表
	 */
	@GetMapping(value="/girls")
	public List<Girl> girlList()
	{
		return girlRepository.findAll();
		
	}
	/**
	 * 添加一个女生
	 */
	@GetMapping(value="/addGirl")
	public Girl addGirl(@RequestParam("age") Integer age,@RequestParam("name") String name,
			@RequestParam("cupSize") String cupSize,@RequestParam("height") double height,
			@RequestParam("weight") double weight)
	{
		Girl girl = new Girl();
		girl.setAge(age);
		girl.setName(name);
		girl.setCupSize(cupSize);
		girl.setHeight(height);
		girl.setWeight(weight);
		
		
		return girlRepository.save(girl);
		
	}
	/**
	 * 查询一个女孩
	 * @param id
	 * @return
	 */
	@GetMapping(value="/findOne/{id}")
	public Optional<Girl> findOne(@PathVariable("id") Integer id)
	{
		return girlRepository.findById(id);
		
	}
	/**
	 * 更新一个女生
	 */
	@PutMapping(value="/updateGirl/{id}")
	public Girl updateGirl(@PathVariable("id") Integer id,
			@RequestParam("name") String name ,@RequestParam("height") double height)
	{
		Girl girl = new Girl();
		girl.setId(id);
		girl.setName(name);
		girl.setHeight(height);
		
		return girlRepository.save(girl);
		
	}
	/**
	 * 删除一条记录
	 */
	@DeleteMapping(value="deleteGirl/{id}")
	public void delete(@PathVariable("id") Integer id)
	{
		girlRepository.deleteById(id);
		
	}
	/**
	 * springboot事务
	 */
	@PostMapping(value="/girlsTwo")
	public void girlsTwo()
	{
		girlService.insertTwo();
		
	}
}
